package com.shike.hq.controller;

import com.shike.hq.entity.Admin;
import com.shike.hq.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;

// 增加indexController用来获取当前用户的session
@Controller
public class IndexController {

    @Autowired
    AdminService adminService;

    @GetMapping("/index")
    public String index(HttpSession session) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(principal);
        Admin adminUser = (Admin) principal;
        System.out.println("登录成功，进入index，获取当前用户信息");
        System.out.println(adminUser);
        Admin admin = adminService.selectAdminByUsername(adminUser.getUsername());
        admin.setLastLogin(new Timestamp(new Date().getTime()));
        adminService.updateById(admin);
        session.setAttribute("admin", admin);
        return "index";
    }

    @GetMapping()
    public String login(){
        return "login";
    }
}
